package com.heima.user.service.impl;

import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.user.dto.UserRepDto;
import com.heima.user.entity.User;
import com.heima.user.mapper.UserMapper;
import com.heima.user.service.IUserService;
import com.heima.user.utils.JwtTool;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.Duration;

@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    private final JwtTool jwtTool;


    @Override
    public String login(UserRepDto dto) {
        // select * from tb_user where username = #{username} and password = #{password}
        User user = lambdaQuery()
                .eq(User::getMobile, dto.getMobile())
                .eq(User::getPwd, dto.getPwd())
                .one();
        if(user==null){
            throw new RuntimeException("用户名或密码错误");
        }

        String token = jwtTool.createToken(user.getId(), Duration.ofHours(2));
        return token;
    }
}
